package com.wudimeng.syll.common;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * Description: {message工具}
 * Date:  2019/4/1
 *
 * @version 0.8
 * @author: wuxianliang
 */

@Component
public class MessageUtil {

    private Properties properties;

    private static final Logger log = LoggerFactory.getLogger(MessageUtil.class);

    private final String DEFAULT_PROPERTY_LOCATION = "/message.properties";

    public MessageUtil(){
        InputStream is = getClass().getResourceAsStream(DEFAULT_PROPERTY_LOCATION);
        if(is == null)return;
        properties = new Properties();
        try {
            properties.load(is);
        } catch (IOException e) {
            log.error("读取properties失败. reason:{}", e.getMessage());
        }
    }

    /**
     *Description:{读取properties中指定key的值}
     *Date:2018/11/7 10:14
     *@param key 键
     *@return String
     *@author wuxianliang
     */
    public String get(String key){
        if (properties == null) {
            return StringUtils.EMPTY;
        }
        String property = properties.getProperty(key);
        return property == null ? key : property;
    }
}
